package com.baiytfp.hf.HFDataExchange.common.utils;

import com.baiytfp.hf.baiytfp.common.db.HFPager;
import com.baiytfp.hf.baiytfp.common.util.DateUtil;
import com.baiytfp.hf.baiytfp.hfutils.HFString;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class HFPagerUtil {

	
	//----------------普通方法
	/**
	 * 从request中取出搜索条件封装到hfPager中
	 * @param hfPager
	 * @param request
	 * @return
	 */
	public static HFPager setParams(HFPager hfPager, HttpServletRequest request) {
		/*Map<String, String> map = new HashMap<String, String>();
		Enumeration<String> parameterNames = request.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String paramName = (String) parameterNames.nextElement();
			String[] paramValues = request.getParameterValues(paramName);
			if (paramValues.length == 1) {
				String paramValue = paramValues[0];
				if (paramValue.length() != 0) {
					map.put(paramName, HFString.getIstance().getString(paramValue, ""));
				}
			}
		}
		hfPager.setParams(map);
		return hfPager;*/
		return setParamsEndDayAmend(hfPager, request, null);
	}
	
	/**
	 * 从request中取出搜索条件封装成一个HFPager
	 * @param request
	 * @return
	 */
	public static HFPager getHFPager(HttpServletRequest request) {
		HFPager hfPager = new HFPager(request);
		return setParams(hfPager, request);
	}
	
	
	//------------------时间修正方法
	/**
	 * 这个方法可以将查询条件中以 _time_end 结尾的时间加一天
	 * 比如 查询12-01 结束的日期条件, 变成12-02
	 * @param hfPager
	 * @param request
	 * @return
	 */
	public static HFPager setParamsEndDayAmend(HFPager hfPager, HttpServletRequest request) {
		return setParamsEndDayAmend(hfPager, request, ".+_time_end");
	}
	
	public static HFPager getHFPagerEndDayAmend(HttpServletRequest request) {
		HFPager hfPager = new HFPager(request);
		return setParamsEndDayAmend(hfPager, request);
	}
	
	//-----------------高级
	/**
	 * 修正符合正则的时间
	 * @param hfPager
	 * @param request
	 * @param regex
	 * @return
	 */
	public static HFPager setParamsEndDayAmend(HFPager hfPager, HttpServletRequest request, String... regex) {
		Map<String, String> map = new HashMap<String, String>();
		Enumeration<String> parameterNames = request.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String paramName = (String) parameterNames.nextElement();
			String[] paramValues = request.getParameterValues(paramName);
			if (paramValues.length == 1) {
				String paramValue = paramValues[0];
				if (paramValue.length() != 0) {
					//修正值
					if(regex != null) {
						for(String r : regex) {
							if(paramName.matches(r)) {	//匹配给定的正则表达式
								try {
									Calendar c = Calendar.getInstance();  
									c.setTime(DateUtil.yyyy_MM_dd_sdf.parse(HFString.getIstance().getString(paramValue, "")));
									c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天  
									map.put(paramName, DateUtil.formatDate(c.getTime()));
								} catch (ParseException e) {
									map.put(paramName, HFString.getIstance().getString(paramValue, ""));
								}  
							} else 
								map.put(paramName, HFString.getIstance().getString(paramValue, ""));
						}
					} else {
						map.put(paramName, HFString.getIstance().getString(paramValue, ""));
					}
				}
			}
		}
		hfPager.setParams(map);
		return hfPager;
	}
	public static HFPager getHFPagerEndDayAmend(HttpServletRequest request, String... regex) {
		HFPager hfPager = new HFPager(request);
		return setParamsEndDayAmend(hfPager, request, regex);
	}
}
